package it.mp.multitask;

import java.lang.reflect.Method;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class DialogoKill extends Activity {

	private String packageName = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialogo_kill);
		Intent intent = getIntent();
		packageName = intent.getExtras().getString(Main.PACKAGE_NAME);
		ImageView iv = (ImageView) findViewById(R.id.IV_application);
		try {
			iv.setImageDrawable(getPackageManager().getApplicationIcon(packageName));
		} catch (NameNotFoundException e) {
		}

		try {
			findViewById(R.id.TV_applicationName).setVisibility(View.VISIBLE);
			((TextView) findViewById(R.id.TV_applicationName)).setText(getPackageManager().getApplicationLabel(
					getPackageManager().getApplicationInfo(packageName, 0)));
		} catch (NameNotFoundException e) {
			findViewById(R.id.TV_applicationName).setVisibility(View.INVISIBLE);
		}

		findViewById(R.id.BTN_cancel).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				finish();
			}
		});
		findViewById(R.id.BTN_kill).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				if (Build.VERSION.RELEASE.equals("2.2")) {
					killApplication2(packageName);
				} else {
					killApplication(packageName);
				}
				finish();
			}
		});

	}
	private void killApplication(String packageName) {
		ActivityManager actvityManager = (ActivityManager) DialogoKill.this.getSystemService(ACTIVITY_SERVICE);
		actvityManager.restartPackage(packageName);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(it.mp.multitask.Main.PREF_KILL_SENZA_CONFERMA, ((CheckBox) findViewById(R.id.CB_question)).isChecked());
		editor.commit();
	}
	private void killApplication2(String packageName) {

		ActivityManager activityManager = (ActivityManager) DialogoKill.this.getSystemService(ACTIVITY_SERVICE);

		Class<? extends ActivityManager> dummyClass = activityManager.getClass();
		Class<?>[] partypes = new Class<?>[]{String.class};
		Method meth;
		try {
			meth = dummyClass.getMethod("killBackgroundProcesses", partypes);
			Object[] arglist = new Object[]{packageName};
			meth.invoke(activityManager, arglist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putBoolean(it.mp.multitask.Main.PREF_KILL_SENZA_CONFERMA, ((CheckBox) findViewById(R.id.CB_question)).isChecked());
		editor.commit();
	}
}
